package com.flying.planewar;

import com.flying.planewar.utils.PlaneUtil;

import java.awt.*;
import java.awt.event.*;
import java.lang.invoke.VarHandle;
import java.util.Date;

import static com.flying.planewar.utils.PlaneUtil.*;

/**
 * @author 刘鹏飞
 * @Classname TestJWT
 * @Date 2024/12/17 13:46
 * @Created by 刘鹏飞
 * @Description
 */
public class MyPlaneGameFrame extends Frame {

    Image bg = PlaneUtil.getImage("images/bg.jpg");
    Image planeImg = PlaneUtil.getImage("images/plane.png");
    Plane plane = new Plane(planeImg, 200, 200, 7);
    Shell[] shells = new Shell[20];
    Explode explode;  // 爆炸对象

    // 定义开始时间和结束时间
    Date startTime = new Date();
    Date endTime;

    // 绘制方法
    @Override
    public void paint(Graphics g) {
        g.drawImage(bg, 0, 0, FRAME_WIDTH, FRAME_HEIGHT, null);
        plane.drawMyself(g);

        //绘制所有炮弹
        for (Shell shell : shells) {
            if (shell == null) {
                return;
            }
            shell.drawMyself(g);


            //飞机和所有炮弹对象进行矩形检测
            boolean peng = shell.getRectangle().intersects(plane.getRectangle());
            if (peng) {
                plane.live = false;  //飞机死掉,画面不显示
                endTime=new Date();
                // 创建爆炸对象
                if (explode == null) {
                    explode = new Explode(plane.x, plane.y);
                }
                explode.draw(g);

                // 计算时间



            }
        }

        // 打印内容
        if(!plane.live){
            if(endTime==null){
                endTime = new Date();
            }
            int period = (int)((endTime.getTime()-startTime.getTime())/1000);
            printInfo(g, "游戏结束! 时间："+period+"秒", 25, 120, 260, Color.white);
        }


    }

    /**
     * 在窗口上打印信息
     *
     * @param g
     */
    public void printInfo(Graphics g, String str, int size, int x, int y, Color color) {


        Font font = new Font("宋体", Font.BOLD, size);
        g.setFont(font);
        g.setColor(color);
        g.drawString(str, x, y);



    }


    // 重画线程 内部类
    class PaintThread extends Thread {
        @Override
        public void run() {
            while (true) {
                repaint();
                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 键盘监听内部类

    class KeyMonitor extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            plane.addDirection(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            plane.minusDirection(e);
        }
    }


    // 初始化窗口
    public void launchFrame() {
        this.setTitle("飞机大战--Flying");
        this.setSize(FRAME_WIDTH, FRAME_HEIGHT);  // 设置窗口大小
        this.setLocation(500, 500);  // 设置窗口位置
        this.setVisible(true); // 设置窗口可见

        // 增加关闭窗口动作

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("窗口已关闭");
                System.exit(0);
            }
        });


        // 启动重画线程
        new PaintThread().start();


        // 开启键盘监听
        this.addKeyListener(new KeyMonitor());

        // 初始化50发炮弹
        for (int i = 0; i < shells.length; i++) {
            shells[i] = new Shell();
        }


    }

    // 启用二级缓存

    private Image offScreenImage = null;

    public void update(Graphics g) {
        if (offScreenImage == null)
            offScreenImage = this.createImage(FRAME_WIDTH, FRAME_HEIGHT);//这是游戏窗口的宽度和高度

        Graphics gOff = offScreenImage.getGraphics();
        paint(gOff);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    public static void main(String[] args) {

        MyPlaneGameFrame myPlaneGameFrame = new MyPlaneGameFrame();

        myPlaneGameFrame.launchFrame();


    }

}
